<?php
  /* 
   * Paging
   */
  header("Content-Type: text/html; charset=UTF-8"); 
  $lbs = new Model('coupon');
   $tj = new SampleModel();
	$where = $tj->load_array_from_get();
	if(trim($where['name']!='')){
		$where['name@~'] = $where['name'];
	}
  unset($where['name']);
  $iTotalRecords = $lbs->count('id');
  $iDisplayLength = intval($_GET['iDisplayLength']);
  $iDisplayLength = $iDisplayLength < 0 ? $iTotalRecords : $iDisplayLength; 
  $iDisplayStart = intval($_GET['iDisplayStart']);
  $sSearch = $_GET['sSearch'];
 
  $sEcho = intval($_GET['sEcho']);
  $end = $iDisplayStart+$iDisplayLength;
  $page=0;
  if($iDisplayStart=='0'||$iDisplayStart==0){
    $page = 1;
  }else{
    $page = (int)($iDisplayStart/$iDisplayLength)+1;
 }
  $startCount = $page*$iDisplayLength;
//  $p = new Pagination($startCount,$iDisplayLength);
//  $res = $p->model_list($lbs->where($where)->order('id desc'));
  $db = DB::get_db();
  if(empty($sSearch)){
      $sql = "select * from coupon ORDER BY id DESC limit ".$iDisplayStart.','.$iDisplayLength;
  }else{
      $sql = "select * from coupon where name like '%".$sSearch."%' ORDER BY id DESC limit ".$iDisplayStart.','.$iDisplayLength;
  }
  $res = $db->query($sql);
  $records = array();
  $records["aaData"] = array(); 
  foreach($res as $r){  
   $records["aaData"][] = array(
      urlencode($r['name']),
      urlencode($r['gjz']),
      $r['kssj'].'-'.$r['jssj']
    );	   
  }  

  $records["sEcho"] = $sEcho;
  $records["iTotalRecords"] = $iTotalRecords;
  $records["iTotalDisplayRecords"] = $iTotalRecords;
  
  echo  urldecode(json_encode($records));
  
  